﻿package GUI {
	import flash.display.MovieClip;
	import com.as3toolkit.events.KeyboarderEvent;
	import com.as3toolkit.ui.Keyboarder;
	import flash.ui.Keyboard;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import caurina.transitions.Tweener;
	import code.Field;
	
	/**
	 * ...
	 * @author Sam Morrison
	 */
	public class Title extends MovieClip {
		
		private var options:Array = new Array();
		private var selected:int;
		
		public function Title() {
			
			options.push("play");
			options.push("tutorial");
			options.push("credits");
			
			addEventListener(Event.ENTER_FRAME, update);
			Keyboarder.Instance.addEventListener(KeyboarderEvent.FIRST_PRESS, pressed);
		}
		
		private function pressed(e:KeyboarderEvent):void {
			
			// Down
			if ((e.keyCode == 83
				|| e.keyCode == Keyboard.DOWN)
				&& selected < options.length - 1) {
				selected++;
			}
			
			// Up
			if ((e.keyCode == 87
				|| e.keyCode == Keyboard.UP)
				&& selected > 0) {
				selected--;
			}
			
			switch (options[selected]) 
			{
				case "play":
					if (e.keyCode == Keyboard.ENTER)
					{
						(parent as code.Field).gameInit();
					}
					// animate out credits
					Tweener.addTween(creditNames_mc, { y: 750, alpha:1, time:.8, transition:"easeOutQuint" } );
				break;
				
				case "tutorial":
					if (e.keyCode == Keyboard.ENTER)
					{
						// Go to tutorial
					}
					// animate out credits
					Tweener.addTween(creditNames_mc, { y: 750, alpha:1, time:.8, transition:"easeOutQuint" } );
				break;
				
				case "credits":
					// Animate in credits
					Tweener.addTween(creditNames_mc, { y: 640, alpha:1, time:.8, transition:"easeOutQuint" } );
				break;
			}
		}
		
		private function update(e:Event):void {
			
			switch (options[selected]) 
			{
				case "play":
					play_mc.gotoAndStop(2);
					tutorial_mc.gotoAndStop(1);
					credits_mc.gotoAndStop(1);
				break;
				
				case "tutorial":
					play_mc.gotoAndStop(1);
					tutorial_mc.gotoAndStop(2);
					credits_mc.gotoAndStop(1);
				break;
				
				case "credits":
					play_mc.gotoAndStop(1);
					tutorial_mc.gotoAndStop(1);
					credits_mc.gotoAndStop(2);
				break;
			}
		}
		
		public function remove():void {
			Keyboarder.Instance.removeEventListener(KeyboarderEvent.FIRST_PRESS, pressed);
			removeEventListener(Event.ENTER_FRAME, update);
		}
		
	}

}